package com.yww.controller;


import com.yww.entity.Roles;
import com.yww.entity.UserRoles;
import com.yww.service.IUserRolesService;
import com.yww.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨雯雯
 * @since 2020-05-20
 */
@RestController
@RequestMapping("/user-roles")
public class UserRolesController {
    @Autowired
    IUserRolesService iUserRolesService;

    /**
     * 根据用户id查询该用户拥有的角色们
     * @param userId
     * @return
     */
    @RequestMapping("/getRolesListByUserId")
    public Object getRolesListByUserId(@RequestParam Integer userId){
        List<Roles> list=iUserRolesService.getRolesListByUserId(userId);
        return list;
    }


    /**
     * 根据用户id批量添加角色
     * @param userId
     * @param ids
     * @return
     */
    @RequestMapping("/addRolesBatchByUserId")
    public Object addRolesBatchByUserId(@RequestParam Integer userId,@RequestParam String ids){
     List<UserRoles> list=new ArrayList<UserRoles>();
     String[] arr= ids.split(",");
     for(String s:arr){
         Integer integer = Integer.valueOf(s);
         UserRoles ur=new UserRoles(0,userId,integer);
         list.add(ur);

     }
        boolean b = iUserRolesService.saveBatch(list);
           if(b){
               return new Result(0,"批量添加成功");
           }
               return new Result(0,"批量添加失败");
    }


    /**
     * 根据用户id批量删除角色
     * @param userId
     * @param ids
     * @return
     */
    @RequestMapping("/delRolesBatchByUserId")
    public Object delRolesBatchByUserId(@RequestParam Integer userId,@RequestParam String ids){
        String[] arr= ids.split(",");
        boolean b = iUserRolesService.delRolesBatchByUserId(userId,ids);
        if(b){
            return new Result(0,"批量删除成功");
        }
        return new Result(0,"批量删除失败");
    }


    /**
     * (查)查询用户角色列表
     * @return
     */
    @RequestMapping("/getAllList")
    public Object getAllList(){
        List<UserRoles> list = iUserRolesService.list();
        return list;
    }

}
